# template for "Guess the number" mini-project
# input will come from buttons and an input field
# all output for the game will be printed in the console

import simplegui
import random

# initialize global variables used in your code
num_range = 100
num_guess = 7
comp_number = 11


# define event handlers for control panel

def init():
    global num_range, num_guess, comp_number
    comp_number = random.randrange(0, num_range)
    
    if num_range == 100 :
        num_guess = 7
    else :
        num_guess = 10
    
    start_message()    

def start_message():
    print ""
    print "Computer has chosen a number from 0 to", num_range
    print "You have", num_guess, "chances to guess the number right"
    print ""

def range100():  
    # button that changes range to range [0,100) and restarts
    global comp_number, num_guess, num_range
    num_range = 100
    comp_number = random.randrange(0, num_range)
    num_guess = 7
    start_message()

def range1000():
    # button that changes range to range [0,1000) and restarts
    global comp_number, num_guess, num_range
    num_range = 1000
    comp_number = random.randrange(0, num_range)
    num_guess = 10
    start_message()
    
def get_input(guess):
    global num_guess
        
    # main game logic goes here
    print "Guess is " + guess
    
    if int(guess) == comp_number :
        print "Correct!"
        print ""
        init()
        return
    
    elif int(guess) > comp_number :
        print "Lower!"
        print ""
           
    else :
        print "Higher!"
        print ""
        
    num_guess = num_guess - 1
    if num_guess == 0:
        print "You ran out of guesses.  The number is", comp_number
        init()
    else :
        print "Number of remaining guess is", num_guess
    
# create frame
frame = simplegui.create_frame("Guess the Number", 300, 300)

# register event handlers for control elements
frame.add_button("Range is [0 to 100)", range100, 200)
frame.add_button("Range is [0 to 1000)", range1000, 200)
frame.add_input("Enter a Guess: ", get_input, 80)

# start frame
init()
frame.start()

# always remember to check your completed program against the grading rubric
